import logging
from django.views.decorators.http import require_GET
from common.dwc.maestro import integrated_analysis_handler
from common.utils.decorator import response_wrapper, validate_form
from common.utils.api import token_required
from common.permission.handler import process_resp_items, get_key_mapper
from common.timeutil import get_interval_end_pair
from common.const import INTERVAL_TYPE_MAPPER
from common.mg_core import mg
from .form import (
    ProductProfitAnalysisForm,
    UserAnalysisForm,
    BetAnalysisForm,
    ChannelAnalysisForm,
    RechargeAnalysisForm,
    WithdrawAnalysisForm
)

_LOGGER = logging.getLogger(__name__)


@require_GET
@response_wrapper
@token_required
@validate_form(ProductProfitAnalysisForm)
def get_product_profit_analysis(req, cleaned_data):
    merchant_name, role_id = req.merchant_name, req.merchant_role['id']
    interval_type = cleaned_data['interval_type']
    target_time = cleaned_data['target_time']
    start_time, _ = get_interval_end_pair(INTERVAL_TYPE_MAPPER[interval_type], target_time, 7, week_start=5)
    end_time, _ = get_interval_end_pair(INTERVAL_TYPE_MAPPER[interval_type], target_time, 0, week_start=5)
    condition = dict(updated_at={'$gte': start_time, '$lte': end_time}, interval_type=interval_type)
    product_profit_report, total_count = mg.get_report(merchant_name, 'integrated_profit_report', condition,
                                                       sort_key='updated_at', page=1, size=100,
                                                       disable_paginate=True)
    key_mapper = get_key_mapper(merchant_name, '5001', role_id)
    resp_items = process_resp_items(product_profit_report, key_mapper)
    resp_items = integrated_analysis_handler.calc_ring_ratio_and_average_stats(resp_items, key_mapper)
    return dict(total_count=total_count, list=resp_items, key_mapper=key_mapper)


@require_GET
@response_wrapper
@token_required
@validate_form(UserAnalysisForm)
def get_user_analysis(req, cleaned_data):
    merchant_name, role_id = req.merchant_name, req.merchant_role['id']
    interval_type = cleaned_data['interval_type']
    report_type = cleaned_data['report_type']
    target_time = cleaned_data['target_time']
    start_time, _ = get_interval_end_pair(INTERVAL_TYPE_MAPPER[interval_type], target_time, 7, week_start=5)
    end_time, _ = get_interval_end_pair(INTERVAL_TYPE_MAPPER[interval_type], target_time, 0, week_start=5)
    condition = dict(updated_at={'$gte': start_time, '$lte': end_time}, interval_type=interval_type)
    report_key = '_'.join(['integrated', 'user', report_type, 'analysis', 'report'])
    user_report, total_count = mg.get_report(merchant_name, report_key, condition,
                                             sort_key='updated_at', page=1, size=100,
                                             disable_paginate=True)
    key_mapper = get_key_mapper(merchant_name, '5002', role_id, report_type=report_type)
    resp_items = process_resp_items(user_report, key_mapper)
    resp_items = integrated_analysis_handler.calc_ring_ratio_and_average_stats(resp_items, key_mapper)
    return dict(total_count=total_count, list=resp_items, key_mapper=key_mapper)


@require_GET
@response_wrapper
@token_required
@validate_form(BetAnalysisForm)
def get_bet_analysis(req, cleaned_data):
    merchant_name, role_id = req.merchant_name, req.merchant_role['id']
    interval_type = cleaned_data['interval_type']
    report_type = cleaned_data['report_type']
    target_time = cleaned_data['target_time']
    start_time, _ = get_interval_end_pair(INTERVAL_TYPE_MAPPER[interval_type], target_time, 7, week_start=5)
    end_time, _ = get_interval_end_pair(INTERVAL_TYPE_MAPPER[interval_type], target_time, 0, week_start=5)
    resp_items = integrated_analysis_handler.get_bet_analysis_report(
        start_time, end_time, interval_type, report_type, merchant_name
    )
    key_mapper = get_key_mapper(merchant_name, '5003', role_id, report_type=report_type)
    resp_items = process_resp_items(resp_items, key_mapper)
    resp_items = integrated_analysis_handler.calc_ring_ratio_and_average_stats(resp_items, key_mapper)
    return dict(list=resp_items, key_mapper=key_mapper)


@require_GET
@response_wrapper
@token_required
@validate_form(ChannelAnalysisForm)
def get_channel_analysis(req, cleaned_data):
    merchant_name, role_id = req.merchant_name, req.merchant_role['id']
    interval_type = cleaned_data['interval_type']
    specific_chn = cleaned_data['specific_chn']
    target_time = cleaned_data['target_time']
    resp_items, total_count = integrated_analysis_handler.get_channel_analysis_report(
        merchant_name, target_time, interval_type, specific_chn
    )
    key_mapper = get_key_mapper(merchant_name, '5004', role_id, specific_chn=specific_chn)
    resp_items = process_resp_items(resp_items, key_mapper)
    if specific_chn:
        resp_items = integrated_analysis_handler.calc_ring_ratio_and_average_stats(resp_items, key_mapper)
    return dict(total_count=total_count, list=resp_items, key_mapper=key_mapper)


@require_GET
@response_wrapper
@token_required
@validate_form(RechargeAnalysisForm)
def get_recharge_analysis(req, cleaned_data):
    merchant_name, role_id = req.merchant_name, req.merchant_role['id']
    interval_type = cleaned_data['interval_type']
    report_type = cleaned_data['report_type']
    target_time = cleaned_data['target_time']
    start_time, _ = get_interval_end_pair(INTERVAL_TYPE_MAPPER[interval_type], target_time, 7, week_start=5)
    end_time, _ = get_interval_end_pair(INTERVAL_TYPE_MAPPER[interval_type], target_time, 0, week_start=5)
    condition = dict(updated_at={'$gte': start_time, '$lte': end_time}, interval_type=interval_type)
    report_key = '_'.join(['integrated', report_type, 'report'])
    recharge_report, total_count = mg.get_report(merchant_name, report_key, condition,
                                                 sort_key='updated_at', page=1, size=100,
                                                 disable_paginate=True)
    key_mapper = get_key_mapper(merchant_name, '5005', role_id, report_type=report_type)
    resp_items = process_resp_items(recharge_report, key_mapper)
    resp_items = integrated_analysis_handler.calc_ring_ratio_and_average_stats(resp_items, key_mapper)
    return dict(total_count=total_count, list=resp_items, key_mapper=key_mapper)


@require_GET
@response_wrapper
@token_required
@validate_form(WithdrawAnalysisForm)
def get_withdraw_analysis(req, cleaned_data):
    merchant_name, role_id = req.merchant_name, req.merchant_role['id']
    interval_type = cleaned_data['interval_type']
    target_time = cleaned_data['target_time']
    start_time, _ = get_interval_end_pair(INTERVAL_TYPE_MAPPER[interval_type], target_time, 7, week_start=5)
    end_time, _ = get_interval_end_pair(INTERVAL_TYPE_MAPPER[interval_type], target_time, 0, week_start=5)
    condition = dict(updated_at={'$gte': start_time, '$lte': end_time}, interval_type=interval_type)
    withdraw_report, total_count = mg.get_report(merchant_name, 'integrated_withdraw_report', condition,
                                                 sort_key='updated_at', page=1, size=20,
                                                 disable_paginate=True)
    key_mapper = get_key_mapper(merchant_name, '5006', role_id)
    resp_items = process_resp_items(withdraw_report, key_mapper)
    resp_items = integrated_analysis_handler.calc_ring_ratio_and_average_stats(resp_items, key_mapper)
    return dict(total_count=total_count, list=resp_items, key_mapper=key_mapper)


